<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Form\Import;
use App\Admin\Extensions\WpsFormExport;
use App\Models\WpsFormAnswer;
use App\Models\WpsFormList;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class WpsFormAnswerController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '收集记录';

    public function title()
    {
        $id = request()->input('wps_form_id');
        return WpsFormList::query()->where('id', $id)->value('title') . '|' . $this->title;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WpsFormAnswer());

        $grid->column('id', __('ID'));
//        $grid->column('title', __('Title'));
//        $grid->column('wps_form_id', __('表单名称'));
//        $grid->column('bind_code', __('Bind code'));
//        $grid->column('event', __('Event'));
        $grid->column('answer', __('提交数据'))->display(function () {
            return '查看详情';
        })->modal('数据', function ($model) {
            $array = [];
            $answers = $this->answer;
            if ($answers && is_array($answers)) {
                foreach ($answers as $key => $answer) {
                    if (is_array($answer) && isset($answer['date'])) {
                        $array[] = [
                            $key, $answer['date']
                        ];
                    } else {
                        if (is_array($answer)) {
                            continue;
                        }
                        $array[] = [
                            $key, $answer
                        ];
                    }
                }
            }
            return new Table(['标题', '内容',], $array);
        });
        $grid->column('submit_time', __('提交时间'));
        $id = request()->input('wps_form_id');
        $grid->model()->where('wps_form_id', $id);
        $grid->disableCreateButton();
//        $grid->disableExport();
        $grid->exporter(new WpsFormExport());
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new Import());
        });
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('answer', __('关键字'))->placeholder('做单群/客户名称/项目名称');

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WpsFormAnswer::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title', __('Title'));
        $show->field('wps_form_id', __('Wps form id'));
        $show->field('bind_code', __('Bind code'));
        $show->field('event', __('Event'));
        $show->field('answer', __('Answer'));
        $show->field('is_del', __('Is del'));
        $show->field('submit_time', __('Submit time'));
        $show->field('create_time', __('Create time'));
        $show->field('update_time', __('Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WpsFormAnswer());

        $form->text('title', __('Title'));
        $form->number('wps_form_id', __('Wps form id'));
        $form->text('bind_code', __('Bind code'));
        $form->text('event', __('Event'));
        $form->textarea('answer', __('Answer'));
        $form->switch('is_del', __('Is del'));
        $form->datetime('submit_time', __('Submit time'))->default(date('Y-m-d H:i:s'));

        return $form;
    }
}
